#!/usr/bin/env python
#

import unittest,sys,os
import numpy as np
from scipy.cluster.vq import vq, kmeans2, whiten
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab

sys.path.append("..")
sys.path.append(os.path.join(".","data"))
sys.path.append(os.path.join("..","SpectralMix"))
#from SpectralMix.MixModel import GaussianMix
from SpectralMix import MixModel
from MixModel.GaussianMix import GaussianMix

## class to test that the kmeans algorithm is functioning
class GaussianMixTest(unittest.TestCase):

    ## the main test method to test if kmeans is working correctly
    def testOneDimensionData(self):
        x1 = np.array([[-0.39,0.12,0.94,1.67,1.76,2.44,3.72,4.28,4.92,5.53]]).transpose()
        x2 = np.array([[ 0.06,0.48,1.01,1.68,1.80,3.25,4.12,4.60,5.28,6.22]]).transpose()
        x = np.vstack([x1,x2])
        k = 2
        gm = GaussianMix(x,2,numRuns=10)
        
        print "estimates from example in book are..."
        print '\tmu', [4.62,1.06]
        print '\tvar', [0.87, 0.77]
        print "\tpi", [0.546,0.454]

        print "my estimates are..."
        print "\tmu", gm.maxEstimates[0]['mu'],gm.maxEstimates[1]['mu']
        print "\tsig", gm.maxEstimates[0]['var'],gm.maxEstimates[1]['var']
        print "\tpi", gm.maxEstimates[0]['pi'],gm.maxEstimates[1]['pi']
        print "\tlikelihood", gm.maxLikelihood

        #print "x1 mean: %s , var: %s"%(x1.mean(),x1.var())
        #print "x2 mean: %s , var: %s"%(x2.mean(),x2.var())

### Run the tests
if __name__ == '__main__':
    unittest.main()
